package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
//菜品实现类
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    //引入菜品口味业务层
    @Autowired
    DishFlavorService dishFlavorService;
    //保存
    @Override
    public void saveWithFlavor(DishDto dishDto) {
        //保存菜品的基本信息到菜品表dish
        this.save(dishDto);
        //菜品的id
        Long dishId = dishDto.getId();
        //菜品口味的添加
        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dishId);
        }
        dishFlavorService.saveBatch(flavors);
    }
    //根据id查询
    @Override
    public DishDto getByIdWithFlavor(Long id) {
        //查询菜品的基本信息，从dish表查询
        Dish dish = this.getById(id);
        //创建一个空的DishDto对象来储存
        DishDto dishDto = new DishDto();
        //吧dish拷贝到dishDto中
        BeanUtils.copyProperties(dish,dishDto);

        //查询当前菜品对象的口味信息
        //创建条件构造器
        LambdaQueryWrapper<DishFlavor> dlqw = new LambdaQueryWrapper<>();
        //设置条件根据dish的id来查询相关的口味
        dlqw.eq(DishFlavor::getDishId,dish.getId());
        //查询到的口味分装到集合中
        List<DishFlavor> flavors = dishFlavorService.list(dlqw);
        //将口味设置到dishDto对象中
        dishDto.setFlavors(flavors);
        //返回dishDto对象
        return dishDto;
    }
    ////修改菜品
    @Override
    public void updateWithFlavor(DishDto dishDto) {
        //更新dish表基本信息
        this.updateById(dishDto);
        //清理当前菜品对应口味数据---dish_flavor表的delete操作
        //创建条件构造器
        LambdaQueryWrapper<DishFlavor> dlqw = new LambdaQueryWrapper<>();
        //设置条件根据dish的id来查询相关的口味
        dlqw.eq(DishFlavor::getDishId,dishDto.getId());
        //删除
        dishFlavorService.remove(dlqw);
        //添加当前提交过来的口味数据---dish_flavor表的insert操作
        Long dishId = dishDto.getId();
        //菜品口味的添加
        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dishId);
        }
        //执行添加
        dishFlavorService.saveBatch(flavors);
    }
    //删除
    @Transactional
    @Override
    public void removeWithDish(List<Long> ids) {
        LambdaQueryWrapper<Dish> dlqw = new LambdaQueryWrapper<>();
        dlqw.in(Dish::getId,ids);
        dlqw.eq(Dish::getStatus,1);
        int count = this.count(dlqw);
        if (count>0){
            throw new CustomException("菜品正在售卖中，不能删除");
        }
        this.removeByIds(ids);
        LambdaQueryWrapper<DishFlavor> flqw = new LambdaQueryWrapper<>();
        flqw.in(DishFlavor::getDishId,ids);
        dishFlavorService.remove(flqw);
    }

}
